1213C - Book Reading - CodeForces Solution


math *1200

Please click on ads to support us..

Python Code:

q=int(input())
for _ in range(q):
    n,m=map(int,input().split())
    x=n//m
    y=x%10
    if (m%10)==0:
        print(0)
        continue
    d=m%10
    if d==5:
        tot=(x//2)*5
        if x%2==1:
            tot+=5
        print(tot)
        continue
    if d%2==0:
        tot=(x//10)*40
        for i in range(y):
            tot+=(m*(i+1))%10
    elif d%2==1:
        tot=(x//10)*45
        for i in range(y):
            tot+=(m*(i+1))%10
    print(tot)

C++ Code:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
const ll N = 1e5 + 7;
const ll M = 1e7 + 7;
ll arr[N];
ll P[N];
#define all(v) sort(v.begin(),v.end())
#define rall(v) sort(v.rbegin(),v.rend())
#define ar(a) a.begin(),a.end()
#define bs binary_search
#define lb lower_bound
#define ub upper_bound
#define rev(a) reverse(a.begin(),a.end())
#define f0(i,a,n) for(int i=a;i<n;i++)
#define f1(i,b,a) for(int i=b;i>=a;i--)
#define bbl(a) __builtin_popcountll(a)
#define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define aaja(v)         \
    for (auto &x : v) \
        cin >> x;
#define cout1(v)             \
    for (auto &x : v)      \
    {                      \
        cout << x << endl; \
    }                      \
    nxt;
#define zout1(v)                                     \
    for (auto &x : v)                               \
    {                                               \
        cout << x.first << " " << x.second << endl; \
    }       

// bool sorting(const pair<ll, ll> &a, const pair<ll, ll> &b)
// {
//    if (a.first>b.first)
//    {
//        return (a.second <= b.second);
//    }
   
  
// }
bool isprime(int n){ if(n==1) return false;if(n==2) return true;
for(int i=2;i*i<=n;i++){if(n%i==0) return false;}return true;}
void xorprop(ll n){if (n % 4 == 1){cout<<1<<endl;}else if (n % 4 == 2){ cout<<n+1<<endl;}if (n % 4 == 3){cout<<0<<endl;}if (n % 4 == 0){cout<<n<<endl;}}
//ll kthNonDivisible(ll N, ll K){   ll L = 1;ll H = LLONG_MAX;ll ans = 0;while (L <= H){ll mid = (L + H) / 2;ll sol = mid - mid / N;if (sol > K){H = mid - 1;}else if (sol < K){ L = mid + 1;}else{ans = mid;H = mid - 1;}}return ans;}
void printbinary(int n){for (int i = 6; i >=0; i--){cout<<((n>>i)&1)<<" ";}cout<<endl;} 
bool isSubstring(string s1, string s2){if (s2.find(s1) != string::npos) return true;return false;}
ll ok(ll n)
    {
        ll sum = 0;
        while (n != 0) {
            sum = sum + n % 10;
            n = n / 10;
        }
        return sum;
    }
void solve(){
    ll n, m;
    cin>>n>>m;
    ll ans = 10*m;
    ll ans1=0;
    f0(i, 1 ,10){
        ll p = m*i;
        ans1+=(p%10);
    }
    ans1=(n/(m*10))*ans1;

    ll ans2 = n%(m*10);
     f0(i, 1 ,10){
        ll p = m*i;
        if (p>ans2)
        {
            break;
        }
        
        ans1+=(p%10);
    }
    cout<<ans1<<endl;

    
}
int32_t main()
{
   fast;
    ll test;
    cin>>test;
    while (test--)
    {
       solve();
    }

   return 0;
}


Comments

Submit
0 Comments
More Questions

394. Decode String
902. Numbers At Most N Given Digit Set
221. Maximal Square
1200. Minimum Absolute Difference
1619B - Squares and Cubes
1619A - Square String
1629B - GCD Arrays
1629A - Download More RAM
1629C - Meximum Array
1629D - Peculiar Movie Preferences
1629E - Grid Xor
1629F1 - Game on Sum (Easy Version)
2148. Count Elements With Strictly Smaller and Greater Elements
2149. Rearrange Array Elements by Sign
2150. Find All Lonely Numbers in the Array
2151. Maximum Good People Based on Statements
2144. Minimum Cost of Buying Candies With Discount
Non empty subsets
1630A - And Matching
1630B - Range and Partition
1630C - Paint the Middle
1630D - Flipping Range
1328A - Divisibility Problem
339A - Helpful Maths
4A - Watermelon
476A - Dreamoon and Stairs
1409A - Yet Another Two Integers Problem
977A - Wrong Subtraction
263A - Beautiful Matrix
180C - Letter